<?php

namespace App\Jobs\RemoveDelCatalogData;

use App\Services\CommonService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;


class DelEmptyTable implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $idsInfo            = CommonService::getPartAndProjectClassifyIds();
        $partCompanyIds     = $idsInfo['part_company_ids'];
        $projectClassifyIds = $idsInfo['project_classify_ids'];

        foreach ($partCompanyIds as $partCompanyId) {
            foreach ($projectClassifyIds as $projectClassifyId) {
                $table1 = "book_titles_{$partCompanyId}_{$projectClassifyId}";
                $table2 = "book_catalogs_{$partCompanyId}_{$projectClassifyId}";
                $table3 = "book_catalog_pdfs_{$partCompanyId}_{$projectClassifyId}";
                if (Schema::hasTable($table1)) {
                    $count = DB::table($table1)->count('id');
                    if (empty($count)) { //如果分表没数据，删除分表
                        Schema::drop($table1);
                    }
                }
                if (Schema::hasTable($table2)) {
                    $count = DB::table($table2)->count('id');
                    if (empty($count)) { //如果分表没数据，删除分表
                        Schema::drop($table2);
                    }
                }
                if (Schema::hasTable($table3)) {
                    $count = DB::table($table3)->count('id');
                    if (empty($count)) { //如果分表没数据，删除分表
                        Schema::drop($table3);
                    }
                }
            }
        }
    }

}
